package com.wyb.dao.impl;

import com.wyb.dao.GoodsDao;
import com.wyb.entity.Goods;
import com.wyb.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    JdbcTemplate template= JDBCUtils.getTemplate();
    @Override
    public List<Goods> selectAllGoods() {
       String sql="select * from t_goods";
        return template.query(sql,new BeanPropertyRowMapper<>(Goods.class));
    }

    @Override
    public int insertGoods(Goods goods) {
        String sql="insert into t_goods(goods_name,stock,cost_price,retail_price,discount,status,category_id,list_time,introduction) values(?,?,?,?,?,?,?,?,?)";
        return template.update(sql,goods.getGoodsName(),goods.getStock(),goods.getCostPrice(),goods.getRetailPrice(),goods.getDiscount(),goods.getStatus(),goods.getCategoryId(),goods.getListTime(),goods.getIntroduction());
    }

    @Override
    public Goods selectGoodsById(int id) {
        String sql="select * from t_goods where goods_id=?";
        return template.query(sql,new BeanPropertyRowMapper<>(Goods.class),id).get(0);
    }

    @Override
    public int deleteGoodsById(int id) {
        String sql="delete from t_goods where goods_id=?";
        return template.update(sql,id);
    }

    @Override
    public int updateGoodsById(Goods goods) {
        String sql="update t_goods set goods_name=?,stock=?,cost_price=?,retail_price=?,discount=?,status=?,category_id=?,list_time=?,introduction=? where goods_id=?";
        return template.update(sql,goods.getGoodsName(),goods.getStock(),goods.getCostPrice(),goods.getRetailPrice(),goods.getDiscount(),goods.getStatus(),goods.getCategoryId(),goods.getListTime(),goods.getIntroduction(),goods.getGoodsId());
    }

    @Override
    public int updateGoodsStatusById(int status, int id) {
        String sql="update t_goods set status=? where goods_id=?";
        return template.update(sql,status,id);
    }

    @Override
    public List<Goods> selectGoodsByGoodsName(String goodsName) {
        String sql="select * from t_goods where goods_name like ?";
        return template.query(sql,new BeanPropertyRowMapper<>(Goods.class),"%"+goodsName+"%");
    }
}
